package com.chennan.algorithm.leetcode.three;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

/**
 * @author chennan
 * @create 2023-03
 */
public class 等差子数组1630 {

//[-12, -9, -3, -12]
//        [-9, -3, -12, -6]
//        [20, -25, -20, -15, -10]
//        [-6, 15, 20, -25, -20, -15, -10]
//        [-20, -15, -10]
//        [-25, -20, -15, -10]

    public static void main(String[] args) {
        boolean check = check(Arrays.asList(-9, -3, -12, -6));
        System.out.println(check);
    }

    public List<Boolean> checkArithmeticSubarrays(int[] nums, int[] l, int[] r) {
        List<Boolean> result = new ArrayList<>();
        for(int i=0;i<l.length;i++){
            int start=l[i];
            int length=Math.min(nums.length,r.length);
            List<Integer> tempList=new ArrayList();
            for(int j=start;j<length;j++){
                tempList.add(nums[j]);
            }
            result.add(check(tempList));
        }
        return result;
    }

    private static boolean check(List<Integer> nums){
        nums.sort(Comparator.comparingInt(o -> o));
        if(nums.size()<=2)return true;
        int difference = nums.get(0)-nums.get(1);
        for (int i = 1; i < nums.size()-1; i++) {
            if(difference!=nums.get(i+1)-nums.get(i)){
                return false;
            }
        }
        return true;
    }
}
